<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
session_start();
$nip = $_SESSION['NIP'];

if(isset($_POST["simpan"]))
{
	$rgroup = $_POST['rgroup'];
	if($rgroup == "3"){
		$detil_pekerjaan = 'Selesai';
	}else{
		$detil_pekerjaan = $_POST['keterangan'];
	}
	$id = $_POST['id_pekerjaan'];
	$lokasi_file = $_FILES['nama_file']['tmp_name'];
	$nama_file   = $_FILES['nama_file']['name'];
	$tipe_file   = $_FILES['nama_file']['type'];
	$ukuran_file = $_FILES['nama_file']['size'];
	
	$nama_baru = preg_replace("/\s+/", "_",$nip."_". $nama_file);
	$direktori = "../../file/$nama_baru";
	$MAX_FILE_SIZE = 50000000; //50MB
	move_uploaded_file($lokasi_file, $direktori);
	
	
	$sql = "INSERT INTO progress_pekerjaan(KETERANGAN,ID_PEKERJAAN,WAKTU_PROGRESS,FILE)
	    VALUES ('$detil_pekerjaan','$id',now(),'$nama_baru')";
		
mysql_select_db('mpd');
$retval = mysql_query( $sql, $conn );

if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
}
if(isset($_POST["simpan"]))
{
	$rgroup = $_POST['rgroup'];
	$id = $_POST['id_pekerjaan'];
	$sql2 = "UPDATE pekerjaan SET ID_STATUS_PEKERJAAN =  '".$rgroup."', WAKTU_SELESAI = now() WHERE  `pekerjaan`.`ID_PEKERJAAN` =".$id."";
	mysql_select_db('mpd');
	$retval2 = mysql_query( $sql2, $conn );
	if($rgroup=="3")
	{
		$date3 = mysql_query("SELECT UNIX_TIMESTAMP(DUE_PEKERJAAN) as due1 FROM pekerjaan WHERE ID_PEKERJAAN =".$id);
		$date4 = mysql_query("SELECT UNIX_TIMESTAMP(WAKTU_SELESAI) as ngumpul1 FROM pekerjaan WHERE ID_PEKERJAAN =".$id);
		$q3 = mysql_fetch_array($date3);
		$q4 = mysql_fetch_array($date4);

		$tgl3 = $q3['due1'];
		$tgl4 = $q4['ngumpul1'];

		$hasil2 = $tgl3/$tgl4;

		if($hasil2 > 1.2){
			$hasil2 = "1.2";
		}	else{
			$hasil2= $hasil2;
		}
		$sql3 = "UPDATE pekerjaan SET SCORE =  '".$hasil2."' WHERE  `pekerjaan`.`ID_PEKERJAAN` =".$id."";
		mysql_select_db('mpd');
		$nilai = mysql_query( $sql3, $conn );
	}
}
header('location:../../view/spm/tl_lihatpekerjaan.php');
//var_dump($id);
?>